package cn.bdqn.controller;

import cn.bdqn.pojo.BackendUser;
import cn.bdqn.service.BackendUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/userController")
public class UserController {
    @Resource
    private BackendUserService backendUserService;
    //跳转到登录页面
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "backend/login";
    }

    //登录
    @RequestMapping("/login")
    public String doLogin(HttpServletRequest request, HttpSession session, @Param("userCode") String userCode, @Param("userPassword") String userPassword) {
        BackendUser backendUser = new BackendUser();
        try {
            backendUser = backendUserService.getLogin(userCode, userPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (backendUser != null) {
            session.setAttribute("backendUser", backendUser);
            return "backend/index";
        } else {
            request.setAttribute("error", "用户名或密码不正确");
            return "backend/login";
        }
    }

    //注销
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "index";
    }

}
